using System;
using System.Collections;
using System.Collections.Generic;
using BitMango;
using BitMango.Diagnostics;
using BMCommon;
using Data;
using GTA;
using Holoville.HOTween;
using SevenPieceGame;
using TMPro;
using UnityEngine;

public class PlaySceneHandler : MonoBehaviour, IGameScene, IHandlerBase
{
	private void Update()
	{
	}

	private void OnEnable()
	{
		if (UserLayerManager.Instance.IsInstallOrLowUser())
		{
			m_ShopEnter.SetActive(true);
		}
		else
		{
			m_ShopEnter.SetActive(false);
		}
		PuzzleCtrl puzzleCtrl = this.puzzleCtrl;
		puzzleCtrl.onPuzzleClear = (Action<int, List<BoardInfo>>)Delegate.Combine(puzzleCtrl.onPuzzleClear, new Action<int, List<BoardInfo>>(this.OnClear));
		PuzzleCtrl puzzleCtrl2 = this.puzzleCtrl;
		puzzleCtrl2.onMouseCount = (Action<int>)Delegate.Combine(puzzleCtrl2.onMouseCount, new Action<int>(this.OnMouseCount));
	}

	private void OnDisable()
	{
		PuzzleCtrl puzzleCtrl = this.puzzleCtrl;
		puzzleCtrl.onPuzzleClear = (Action<int, List<BoardInfo>>)Delegate.Remove(puzzleCtrl.onPuzzleClear, new Action<int, List<BoardInfo>>(this.OnClear));
		PuzzleCtrl puzzleCtrl2 = this.puzzleCtrl;
		puzzleCtrl2.onMouseCount = (Action<int>)Delegate.Remove(puzzleCtrl2.onMouseCount, new Action<int>(this.OnMouseCount));
	}

	public void DoDataExchange()
	{
	}

	public void OnEnterScene(object param)
	{
		DataContainer<Data.Root>.Data.userdata.record.lastPlayed = GameMode.packageMode;
		DataContainer<Data.Root>.Data.userdata.SaveContext();
		this.title.text = string.Format("{0} - {1}", DataContainer<Data.Root>.Data.currentPackage.display, DataContainer<Data.Root>.Data.currentLevel.num + 1);
		this.LoadLevel();
	}

	public void OnLeaveScene()
	{
		DataContainer<Data.Root>.Data.userdata.SaveContext();
		this.puzzleCtrl.RemoveBoard();
	}

	private void InitMapInfo()
	{
		float bannerPositionY = BlockBannerAnchor.GetBannerPositionY();
		this.btnBottom.transform.localPosition = new Vector3(this.btnBottom.transform.localPosition.x, bannerPositionY, this.btnBottom.transform.localPosition.z);
		this.bottom.transform.localPosition = new Vector3(this.bottom.transform.localPosition.x, this.bottomDown.transform.parent.localPosition.y + this.bottomDown.transform.localPosition.y, this.bottom.transform.localPosition.z);
		this.top.transform.localPosition = new Vector3(this.top.transform.localPosition.x, BlockBannerAnchor.GetTopPositionY(), this.top.transform.localPosition.z);
		float y = (this.top.transform.localPosition.y + this.topDown.transform.localPosition.y + this.bottomUp.transform.parent.localPosition.y + this.bottomUp.transform.localPosition.y) / 2f;
		this.puzzleCtrl.transform.localPosition = new Vector3(this.puzzleCtrl.transform.localPosition.x, y, this.puzzleCtrl.transform.localPosition.z);
		BlocksCoordinate.coordinate.coordinate.width = DataContainer<Data.Root>.Data.currentLevel.width;
		BlocksCoordinate.coordinate.coordinate.height = DataContainer<Data.Root>.Data.currentLevel.height;
		float num = this.topDown.transform.position.y - this.bottomUp.transform.position.y;
		float num2 = 0f;
		if (GameMode.gameMode == GameType.HEXA || GameMode.gameMode == GameType.RHOMBUS)
		{
			num2 = BlocksCoordinate.coordinate.coordinate.SizeY / 2f;
		}
		BlocksCoordinate.coordinate.coordinate.size = BlocksCoordinate.coordinate.defaultCoordinate.size;
		if (BlocksCoordinate.coordinate.coordinate.SizeY * (float)DataContainer<Data.Root>.Data.currentLevel.height + num2 > num)
		{
			BlocksCoordinate.coordinate.coordinate.size = num / (float)DataContainer<Data.Root>.Data.currentLevel.height / (BlocksCoordinate.coordinate.coordinate.SizeY / BlocksCoordinate.coordinate.coordinate.size) - num2 / (float)DataContainer<Data.Root>.Data.currentLevel.height;
		}
		float num3 = (float)DataContainer<Data.Root>.Data.currentLevel.width;
		if (GameMode.gameMode == GameType.TANGRAM)
		{
			num3 += 2f;
		}
		else if (GameMode.gameMode == GameType.TRIANGLE)
		{
			num3 += 1f;
		}
		else if (GameMode.gameMode == GameType.RHOMBUS)
		{
			num3 *= 0.6666667f;
		}
		float num4 = Mathf.Abs(BlockBannerAnchor.GetRightPositionX() - BlockBannerAnchor.GetLeftPositionX());
		if (BlocksCoordinate.coordinate.coordinate.SizeX * num3 > num4)
		{
			BlocksCoordinate.coordinate.coordinate.size = num4 / num3 / (BlocksCoordinate.coordinate.coordinate.SizeX / BlocksCoordinate.coordinate.coordinate.size);
		}
	}

	private void LoadLevel()
	{
		this.resetCount = 0;
		int num = 20;
		DataContainer<Data.Root>.Data.currentPackage.record.page = DataContainer<Data.Root>.Data.currentLevel.num / num;
		DataContainer<Data.Root>.Data.currentPackage.SaveContext();
		GameMode.gameMode = (GameType)DataContainer<Data.Root>.Data.currentLevel.mode;
		if (GameMode.gameMode == GameType.TRIANGLE)
		{
			BlocksCoordinate.coordinate = new BlockTriangleCoordinate();
		}
		else if (GameMode.gameMode == GameType.SQUARE)
		{
			BlocksCoordinate.coordinate = new BlockSquareCoordinate();
		}
		else if (GameMode.gameMode == GameType.HEXA)
		{
			BlocksCoordinate.coordinate = new BlockHexaCoordinate();
		}
		else if (GameMode.gameMode == GameType.RHOMBUS)
		{
			BlocksCoordinate.coordinate = new BlockRhombusCoordinate();
		}
		else if (GameMode.gameMode == GameType.TANGRAM)
		{
			BlocksCoordinate.coordinate = new BlockTangramCoordinate();
		}
		this.collection.SetActive(!string.IsNullOrEmpty(DataContainer<Data.Root>.Data.currentLevel.pictureTheme));
		this.InitMapInfo();
		this.DwattStart();
		this.puzzleCtrl.LoadLevel(DataContainer<Data.Root>.Data.currentLevel, false, false);
	}

	private void SetVideoAds()
	{
		/*if (RewardAd.IsAdInterstitialReward("PlayScene"))
		{
			this.freeCoin.SetSprite("btn_reward_act");
			this.freeCoin.gameObject.GetComponent<BoxCollider>().enabled = true;
		}
		else
		{
			this.freeCoin.SetSprite("btn_reward_inact");
			this.freeCoin.gameObject.GetComponent<BoxCollider>().enabled = false;
		}
		this.freeCoinRemainCount.text = string.Format("{0}", RewardAd.GetLimitCount("PlayScene") - RewardAd.GetRewardCount("PlayScene"));*/
	}

	private void OnClickVideoAds()
	{
		/*BMSoundHelper.Play("se_btnclick", false, 1f);
		this.isVideoAdsQuit = true;
		RewardAd.ShowVideoAds("PlayScene", delegate(bool isSuccess)
		{
			if (isSuccess)
			{
				int num = 10;
				Wallet.Gain("blockcoin", num, null);
				Wallet.Save();
				this.SetVideoAds();
				this.coinInfo.CoinAnimation(this.cam.transform.InverseTransformPoint(this.freeCoin.transform.position), 5, 0.15f, num, delegate
				{
					this.SetVideoAds();
				});
			}
		});*/
	}

	private void OnClickReply()
	{
		this.puzzleCtrl.UseReply();
		this.resetCount++;
		if (this.resetCount == 2)
		{
			this.resetCount = 0;
		}
		DWATTHelper.logEvent("UseReply", new object[]
		{
			GameMode.packageMode + "_" + DataContainer<Data.Root>.Data.currentPackage.name,
			DataContainer<Data.Root>.Data.currentLevel.num.ToString()
		});
	}

	private void OnClickHome()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		Scene.ChangeTo("IntroScene", null);
	}

	private void OnClickBack()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		Scene.ChangeTo("LevelScene", null);
	}

	private void OnClickSetting()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		this.ShowSettingPopup();
	}

	private void OnClickHintCM()
	{
		BMSoundHelper.Play("se_usehint", false, 1f);
		bool flag = this.puzzleCtrl.UseHint();
		if (flag)
		{
			ADSHelper.ShowRewardedVideoAd((status) =>
			{
				if (status)
				{
					Wallet.Use("blockcoin", 50, null);
					Wallet.Save();
					EconomyLog.SendEconomyLog(LOG_FROM.USE_HINT, LOG_TYPE.CONSUMPTION, "blockcoin", 50);
					DWATTHelper.logEvent("UseHint", new object[]
					{
						GameMode.packageMode + "_" + DataContainer<Data.Root>.Data.currentPackage.name,
						DataContainer<Data.Root>.Data.currentLevel.num.ToString()
					});
				}
			}, ERVType.SevenPiece.ToString());	
		}
	}
	
	private void OnClickHint()
	{
		BMSoundHelper.Play("se_usehint", false, 1f);
		if (Wallet.GetItemCount("blockcoin") >= 50)
		{
			bool flag = this.puzzleCtrl.UseHint();
			if (flag)
			{
				Wallet.Use("blockcoin", 50, null);
				Wallet.Save();
				EconomyLog.SendEconomyLog(LOG_FROM.USE_HINT, LOG_TYPE.CONSUMPTION, "blockcoin", 50);
				DWATTHelper.logEvent("UseHint", new object[]
				{
					GameMode.packageMode + "_" + DataContainer<Data.Root>.Data.currentPackage.name,
					DataContainer<Data.Root>.Data.currentLevel.num.ToString()
				});
			}
		}
		else
		{
			// Scene.QueuePopup("StorePopup");
			// todo show no money
		}
	}

	private void OnClickLevelClear()
	{
	}

	private void OnClickPackageClear()
	{
		MainManager.Instance.ShowUIShop();
	}

	private void OnClear(int moveCount, List<BoardInfo> boardInfos)
	{
		Debugger.Assert(moveCount >= 0, "PlaySceneHandler moveCount >= 0");
		bool isPackageClear = false;
		bool isFirstClear = !DataContainer<Data.Root>.Data.currentLevel.record.isClear;
		DWATTHelper.logEvent("MoveCount", new object[]
		{
			GameMode.packageMode + "_" + DataContainer<Data.Root>.Data.currentPackage.name,
			DataContainer<Data.Root>.Data.currentLevel.num.ToString(),
			moveCount.ToString()
		});
		DataContainer<Data.Root>.Data.currentLevel.record.isClear = true;
		if (DataContainer<Data.Root>.Data.packages[0] != null && DataContainer<Data.Root>.Data.currentPackage == DataContainer<Data.Root>.Data.packages[0])
		{
			if (DataContainer<Data.Root>.Data.currentPackage.levels[0] != null && DataContainer<Data.Root>.Data.currentLevel == DataContainer<Data.Root>.Data.currentPackage.levels[0])
			{
				SingletonMonoBehaviour<Director>.Instance.AppsFlyerLogEvent("af_level1");
			}
			else if (DataContainer<Data.Root>.Data.currentPackage.levels[5] != null && DataContainer<Data.Root>.Data.currentLevel == DataContainer<Data.Root>.Data.currentPackage.levels[5])
			{
				SingletonMonoBehaviour<Director>.Instance.AppsFlyerLogEvent("af_level6");
			}
		}
		if (DataContainer<Data.Root>.Data.currentLevel.num + 1 < DataContainer<Data.Root>.Data.currentPackage.levels.Length)
		{
			if (!DataContainer<Data.Root>.Data.currentPackage.record.isLocked || DataContainer<Data.Root>.Data.currentLevel.num < 4)
			{
				DataContainer<Data.Root>.Data.currentPackage.levels[DataContainer<Data.Root>.Data.currentLevel.num + 1].record.locked = false;
				DataContainer<Data.Root>.Data.currentPackage.levels[DataContainer<Data.Root>.Data.currentLevel.num + 1].SaveContext();
			}
		}
		DataContainer<Data.Root>.Data.currentLevel.SaveContext();
		if (DataContainer<Data.Root>.Data.currentLevel.num + 1 >= DataContainer<Data.Root>.Data.currentPackage.levels.Length && !DataContainer<Data.Root>.Data.currentPackage.record.isClear)
		{
			DataContainer<Data.Root>.Data.currentPackage.record.isClear = true;
			DataContainer<Data.Root>.Data.currentPackage.SaveContext();
			isPackageClear = true;
			this.UnLockNextPackage();
		}
		this.curtain.SetActive(true);
		base.StartCoroutine(this.coClearPopupOpen(boardInfos, isFirstClear, isPackageClear));
	}

	private void UnLockNextPackage()
	{
		int num = Array.FindIndex<Package>(DataContainer<Data.Root>.Data.packages, (Package item) => item == DataContainer<Data.Root>.Data.currentPackage);
		for (int i = num; i < DataContainer<Data.Root>.Data.packages.Length; i++)
		{
			if (i != num && DataContainer<Data.Root>.Data.packages[i].mode.Equals(GameMode.packageMode))
			{
				if (!DataContainer<Data.Root>.Data.packages[i].isPremium && DataContainer<Data.Root>.Data.packages[i].record.isLocked)
				{
					UnityEngine.Debug.Log(DataContainer<Data.Root>.Data.packages[i].name);
					DataContainer<Data.Root>.Data.packages[i].record.isLocked = false;
					DataContainer<Data.Root>.Data.packages[i].SaveContext();
				}
				return;
			}
		}
	}

	private IEnumerator coClearPopupOpen(List<BoardInfo> boardInfos, bool isFirstClear, bool isPackageClear)
	{
		float clearTime = this.puzzleCtrl.ClearAnimation();
		yield return new WaitForSeconds(clearTime);
		Scene.QueuePopup("ClearPopup", new object[]
		{
			boardInfos,
			isFirstClear,
			DataContainer<Data.Root>.Data.currentLevel,
			isPackageClear
		}, delegate(object isOk)
		{
			this.DwattEnd(true);
			if (isOk != null && (bool)isOk)
			{
				if (isPackageClear && DataContainer<Data.Root>.Data.currentPackage.levels.Length >= 40 && !DataContainer<Data.Root>.Data.userdata.record.isReviewed)
				{
					Director.isShowReviewPopup = true;
				}
				this.OnCloseClearPopup();
			}
		});
		yield break;
	}

	private void OnCloseClearPopup()
	{
		if (ClearPopupHandler.closeMessage == "Next")
		{
			if (DataContainer<Data.Root>.Data.currentPackage.record.isLocked && DataContainer<Data.Root>.Data.currentLevel.num >= 4)
			{
				Scene.ChangeTo("LevelScene", null);
				return;
			}
			if (DataContainer<Data.Root>.Data.currentLevel.num >= DataContainer<Data.Root>.Data.currentPackage.levels.Length - 1)
			{
				this.DwattEnd(false);
				Scene.ChangeTo("PackageScene", null);
				return;
			}
			DataContainer<Data.Root>.Data.currentLevel = DataContainer<Data.Root>.Data.currentPackage.levels[DataContainer<Data.Root>.Data.currentLevel.num + 1];
			this.title.text = string.Format("{0} - {1}", DataContainer<Data.Root>.Data.currentPackage.display, DataContainer<Data.Root>.Data.currentLevel.num + 1);
			this.LoadLevel();
		}
		else if (ClearPopupHandler.closeMessage == "NextPackage")
		{
			this.title.text = string.Format("{0} - {1}", DataContainer<Data.Root>.Data.currentPackage.display, DataContainer<Data.Root>.Data.currentLevel.num + 1);
			this.LoadLevel();
		}
		else if (ClearPopupHandler.closeMessage == "Back")
		{
			Scene.ChangeTo("LevelScene", null);
		}
	}

	private void OnMouseCount(int moveCount)
	{
		Debugger.Assert(moveCount >= 0, "PlaySceneHandler moveCount >= 0");
		if (moveCount == 0 || moveCount % (DataContainer<Data.Root>.Data.currentLevel.blockcount * 4) == 0)
		{
		}
	}

	private void HintTongTong()
	{
		this.hintPosition.gameObject.Bounce(1f, 2, 1f, null);
	}

	private void OnApplicationPause(bool pause)
	{
	}

	private void OnEscape()
	{
		if (!this.curtain.activeSelf)
		{
			this.OnClickBack();
		}
	}

	private void ShowSettingPopup()
	{
		Scene.QueuePopup("SettingPopup", true, delegate(object message)
		{
			if (message != null)
			{
				if (((string)message).Equals("BACK"))
				{
					this.DwattEnd(false);
					Scene.ChangeTo("IntroScene", null);
				}
				else if (((string)message).Equals("CLOSE"))
				{
				}
			}
		});
		this.isVideoAdsQuit = false;
	}

	private int GetPackIdx()
	{
		for (int i = 0; i < DataContainer<Data.Root>.Data.packages.Length; i++)
		{
			if (DataContainer<Data.Root>.Data.packages[i] == DataContainer<Data.Root>.Data.currentPackage)
			{
				return i;
			}
		}
		return 0;
	}

	private void DwattStart()
	{
		string text = DataContainer<Data.Root>.Data.currentLevel.author;
		if (string.IsNullOrEmpty(text))
		{
			text = "unknown";
		}
		string stage = string.Format("000_NORMAL/{0}_{1}/{2}@{3}", new object[]
		{
			this.GetPackIdx().ToString("D3"),
			DataContainer<Data.Root>.Data.currentPackage.name,
			DataContainer<Data.Root>.Data.currentLevel.num + 1,
			text
		});
		if (PlaySceneHandler.isProgressLevel)
		{
			this.DwattEnd(false);
		}
		DWATTHelper.startStageEvent(stage);
		DWATTHelper.logEvent("PlayMode", new object[]
		{
			GameMode.packageMode
		});
		PlaySceneHandler.isProgressLevel = true;
	}

	private int GetPackgeIdx()
	{
		for (int i = 0; i < DataContainer<Data.Root>.Data.packages.Length; i++)
		{
			if (DataContainer<Data.Root>.Data.packages[i] == DataContainer<Data.Root>.Data.currentPackage)
			{
				return i;
			}
		}
		return 0;
	}

	private int GetStackedLevelIdx()
	{
		int num = DataContainer<Data.Root>.Data.currentLevel.num;
		int packgeIdx = this.GetPackgeIdx();
		int num2 = 0;
		for (int i = 0; i < packgeIdx; i++)
		{
			num2 += DataContainer<Data.Root>.Data.packages[i].levels.Length;
		}
		return num2 + num + 1;
	}

	private void DwattEnd(bool isSuccess)
	{
		int stackedLevelIdx = this.GetStackedLevelIdx();
		if (isSuccess)
		{
			float num = (float)this.puzzleCtrl.MoveCount / (float)this.puzzleCtrl.GetMinMoves;
			if (num > 1f)
			{
				num *= 25f;
				num = Mathf.Max(1f, num);
				num = Mathf.Min(num, 99f);
				num = (float)((int)num);
			}
			DWATTHelper.logEvent("LevelQuality", new object[]
			{
				stackedLevelIdx,
				1,
				num,
				DataContainer<Data.Root>.Data.currentPackage.name
			});
		}
		else
		{
			float num;
			if (this.puzzleCtrl.MoveCount == 0)
			{
				num = -99f;
			}
			else
			{
				num = -(99f - ((float)this.puzzleCtrl.MoveCount + 1f) / (float)this.puzzleCtrl.GetMinMoves * 99f);
				num = Mathf.Max(-99f, num);
				num = Mathf.Min(num, -1f);
				num = (float)((int)num);
			}
			DWATTHelper.logEvent("LevelQuality", new object[]
			{
				stackedLevelIdx,
				0,
				num,
				DataContainer<Data.Root>.Data.currentPackage.name
			});
		}
		if (PlaySceneHandler.isProgressLevel)
		{
			if (isSuccess)
			{
				DWATTHelper.endStageEvent(true, 0, 0f);
			}
			else
			{
				DWATTHelper.endStageEvent(false, -1, 0f);
			}
			PlaySceneHandler.isProgressLevel = false;
		}
	}

	[SerializeField]
	private tk2dCamera cam;

	[SerializeField]
	private GameObject curtain;

	private static bool isProgressLevel;

	public TextMeshPro title;

	public GameObject top;

	public GameObject topDown;

	public GameObject bottom;

	public GameObject bottomUp;

	public GameObject bottomDown;

	public GameObject btnBottom;

	public PuzzleCtrl puzzleCtrl;

	public GameObject hintPosition;

	public TopOverlayHandler coinInfo;

	public GameObject collection;

	public tk2dSprite freeCoin;

	public TextMeshPro freeCoinRemainCount;

	private Vector3? posBeforeBounce;

	private Tweener bounceAnimation;

	private bool isVideoAdsQuit;

	private int resetCount;

	public GameObject m_ShopEnter;
}
